package com.iotechn.unimall.admin.api.inspection;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dobbinsoft.fw.core.exception.AdminServiceException;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.support.model.Page;
import com.iotechn.unimall.data.domain.InspectionRoundsDO;
import com.iotechn.unimall.data.mapper.InspectionRoundsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.ibatis.session.RowBounds;
import java.util.Date;
import java.util.List;


/**
 * Generate Code By Unimall
 */
@Service
public class inspectionRoundsServiceImpl implements inspectionRoundsService {

    @Autowired
    private InspectionRoundsMapper inspectionRoundsMapper;

    @Override
    public List<InspectionRoundsDO> appRecordRounds(String date,Long adminId) throws ServiceException {
        List<InspectionRoundsDO> inspectionRoundsDOS = inspectionRoundsMapper.selectList(new QueryWrapper<InspectionRoundsDO>()
                .like("inspection_start",date).like("inspection_end",date).orderByAsc("rounds"));
        return inspectionRoundsDOS;
    }


    @Override
    public boolean delete(Long id,  Long adminId) throws ServiceException {
        return inspectionRoundsMapper.deleteById(id) > 0;
    }

    @Override
    public Page<InspectionRoundsDO> list(Date inspectionStart, Integer page, Integer limit, Long adminId) throws ServiceException {
        QueryWrapper<InspectionRoundsDO> wrapper = new QueryWrapper<InspectionRoundsDO>();
        if (inspectionStart != null) {
            wrapper.eq("inspection_start", inspectionStart);
        }
        return inspectionRoundsMapper.selectPage(Page.div(page, limit, InspectionRoundsDO.class), wrapper.orderByDesc("id"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public InspectionRoundsDO create(Long adminId) throws ServiceException {
         Date now = new Date();
         InspectionRoundsDO insertDO = new InspectionRoundsDO();
         insertDO.setGmtUpdate(now);
         insertDO.setGmtCreate(now);
         if (inspectionRoundsMapper.insert(insertDO) > 0) {
             return insertDO;
         }
         throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(Long id, Long adminId) throws ServiceException {
         Date now = new Date();
         InspectionRoundsDO updateDO = new InspectionRoundsDO();
         updateDO.setId(id);
         updateDO.setGmtUpdate(now);
         if (inspectionRoundsMapper.updateById(updateDO) > 0) {
             return "ok";
         }
         throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

}
